package yuan.zk.node;

import org.apache.zookeeper.AsyncCallback;
import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.Watcher;
import org.apache.zookeeper.ZooKeeper;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.util.concurrent.CountDownLatch;

/**
 * @version: 1.0
 * @Description: TODO
 * @author: zZmH
 * @date: 2020/09/11 16:05
 */


public class ZKDel {
    public static final String ip = "192.168.190.128:2181";
    public static final int TIME_OUT = 5000;
    private ZooKeeper zooKeeper;

    @Before
    public void before() throws Exception {
        CountDownLatch latch = new CountDownLatch(1);
        zooKeeper = new ZooKeeper(ip, TIME_OUT, new Watcher() {
            @Override
            public void process(WatchedEvent watchedEvent) {
                if (watchedEvent.getState() == Event.KeeperState.SyncConnected) {
                    System.out.println("connection successfully");
                    latch.countDown();
                }
            }

        });
        latch.await();
    }
    @After
    public void after() throws Exception{
        zooKeeper.close();
    }

    @Test
    public void delete1()throws Exception{
        zooKeeper.delete("/create/node1", 2,
                new AsyncCallback.VoidCallback() {
                    @Override
                    public void processResult(int i, String s, Object o) {
                        System.out.println(i);
                        System.out.println(s);
                    }
                },
                "i am context");
        Thread.sleep(1000);
        System.out.println("end");
    }

    @Test
    public void delete2()throws Exception{
        zooKeeper.delete("/node5", 0);
        System.out.println("end");
    }

}
